package com.cykj.pos.service.impl;

import com.cykj.pos.domain.dto.MsgCashoutRecordDTO;
import com.cykj.pos.profit.dto.MessageDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.apache.commons.lang3.StringUtils;
import com.cykj.pos.mapper.BizCashoutRecordsMapper;
import com.cykj.pos.domain.BizCashoutRecords;
import com.cykj.pos.service.IBizCashoutRecordsService;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 提现记录Service业务层处理
 *
 * @author ningbingwu
 * @date 2021-01-27
 */
@Service
public class BizCashoutRecordsServiceImpl extends ServiceImpl<BizCashoutRecordsMapper, BizCashoutRecords> implements IBizCashoutRecordsService {

    @Resource
    BizCashoutRecordsMapper cashoutRecordsMapper;

    @Override
    public List<BizCashoutRecords> queryList(BizCashoutRecords bizCashoutRecords) {
        LambdaQueryWrapper<BizCashoutRecords> lqw = Wrappers.lambdaQuery();
        if (bizCashoutRecords.getWalletId() != null){
            lqw.eq(BizCashoutRecords::getWalletId ,bizCashoutRecords.getWalletId());
        }
        if (bizCashoutRecords.getCashoutAmount() != null){
            lqw.eq(BizCashoutRecords::getCashoutAmount ,bizCashoutRecords.getCashoutAmount());
        }
        if (StringUtils.isNotBlank(bizCashoutRecords.getDealAccount())){
            lqw.eq(BizCashoutRecords::getDealAccount ,bizCashoutRecords.getDealAccount());
        }
        if (StringUtils.isNotBlank(bizCashoutRecords.getDealBank())){
            lqw.eq(BizCashoutRecords::getDealBank ,bizCashoutRecords.getDealBank());
        }
        if (StringUtils.isNotBlank(bizCashoutRecords.getPayAccount())){
            lqw.eq(BizCashoutRecords::getPayAccount ,bizCashoutRecords.getPayAccount());
        }
        if (StringUtils.isNotBlank(bizCashoutRecords.getPayAccountBank())){
            lqw.eq(BizCashoutRecords::getPayAccountBank ,bizCashoutRecords.getPayAccountBank());
        }
        if (StringUtils.isNotBlank(bizCashoutRecords.getCashoutType())){
            lqw.eq(BizCashoutRecords::getCashoutType ,bizCashoutRecords.getCashoutType());
        }
        if (StringUtils.isNotBlank(bizCashoutRecords.getCashoutStatus())){
            lqw.eq(BizCashoutRecords::getCashoutStatus ,bizCashoutRecords.getCashoutStatus());
        }
        if (StringUtils.isNotBlank(bizCashoutRecords.getVar1())){
            lqw.eq(BizCashoutRecords::getVar1 ,bizCashoutRecords.getVar1());
        }
        if (StringUtils.isNotBlank(bizCashoutRecords.getVar2())){
            lqw.eq(BizCashoutRecords::getVar2 ,bizCashoutRecords.getVar2());
        }
        if (StringUtils.isNotBlank(bizCashoutRecords.getVar3())){
            lqw.eq(BizCashoutRecords::getVar3 ,bizCashoutRecords.getVar3());
        }
        if (StringUtils.isNotBlank(bizCashoutRecords.getVar4())){
            lqw.eq(BizCashoutRecords::getVar4 ,bizCashoutRecords.getVar4());
        }
        if (StringUtils.isNotBlank(bizCashoutRecords.getVar5())){
            lqw.eq(BizCashoutRecords::getVar5 ,bizCashoutRecords.getVar5());
        }
        return this.list(lqw);
    }

    @Override
    public BizCashoutRecords getCashoutRecordsByCustBatchNo(String custBatchNo) {
        LambdaQueryWrapper<BizCashoutRecords> lqw = Wrappers.lambdaQuery();
        lqw.eq(BizCashoutRecords::getVar1 ,custBatchNo);
        return this.getOne(lqw);
    }

    @Override
    public MsgCashoutRecordDTO getCashoutRecordByMsgId(MessageDTO messageDTO) {
        BizCashoutRecords cashoutRecords = cashoutRecordsMapper.selectCashoutRecordByMsgId(messageDTO);
        MsgCashoutRecordDTO msgCashoutRecordDTO = new MsgCashoutRecordDTO();
        //组装数据
        String cardNo = cashoutRecords.getDealAccount();
        if(!"".equals(cardNo) && cardNo.length()>4){
            cardNo = cardNo.substring(cardNo.length()-4);
        }
        String receiptAccount = cashoutRecords.getDealBank()+"（尾号"+cardNo+"）"; //收款银行
        // 提现金额
        BigDecimal cashoutAmount = cashoutRecords.getCashoutAmount();
        BigDecimal serverFee = new BigDecimal(cashoutRecords.getVar3()); //服务费
        BigDecimal rateFee = new BigDecimal(cashoutRecords.getVar4()); //费率金额
        BigDecimal totalServerFee = serverFee.add(rateFee);
        BigDecimal intoAmount = cashoutAmount.subtract(totalServerFee);
        //  创建时间
        SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String createTime = sf.format(cashoutRecords.getCreateTime());
        // 设置到模型中
        msgCashoutRecordDTO.setReceiptAccount(receiptAccount);
        msgCashoutRecordDTO.setIntoAmount(intoAmount);
        msgCashoutRecordDTO.setCreateTime(createTime);
        if(messageDTO.getMsgType()==5){
            // 添加一个错误信息
            msgCashoutRecordDTO.setErrorMsg(cashoutRecords.getErrorMsg());
        }
        return msgCashoutRecordDTO;
    }

    @Override
    public BizCashoutRecords getCashoutRecordsByCustOrderNo(Object orderNo) {
        LambdaQueryWrapper<BizCashoutRecords> lqw = Wrappers.lambdaQuery();
        lqw.eq(BizCashoutRecords::getVar2 ,orderNo);
        return this.getOne(lqw);
    }

    @Override
    public BizCashoutRecords getCashoutDataByBatchId(String batchNo) {
        return cashoutRecordsMapper.getCashoutDataByBatchId(batchNo);
    }

    @Override
    public String getPathNoEnable() {
        return cashoutRecordsMapper.getPathNoEnable();
    }

    @Override
    public String getPathNoEnableLzf() {
        return cashoutRecordsMapper.getPathNoEnableLzf();
    }
}
